#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static int64_t magic_sum(const vector<int>& v) {
    const int oa = v[0], ia = v[1];
    const int ob = v[2], ib = v[3];
    const int oc = v[4], ic = v[5];
    const int od = v[6], id = v[7];
    const int oe = v[8], ie = v[9];
    if (ia == 10 || ib == 10 || ic == 10 || id == 10 || ie == 10)
        return 0;

    const int sa = oa + ia + ib;
    const int sb = ob + ib + ic;
    const int sc = oc + ic + id;
    const int sd = od + id + ie;
    const int se = oe + ie + ia;
    if (!(sa == sb && sa == sc && sa == sd && sa == se)) 
        return 0;

    const vector<vector<int>> m {
        {oa, ia, ib},
        {ob, ib, ic},
        {oc, ic, id},
        {od, id, ie},
        {oe, ie, ia},
    };
    int base = 0;
    for (int i = 0; i < 5; ++i) {
        if (m[i][0] < m[base][0])
            base = i;
    }

    string s;
    for (int i = 0; i < 5; ++i) {
        int j = (base + i) % 5;
        s += to_string(m[j][0]);
        s += to_string(m[j][1]);
        s += to_string(m[j][2]);
    }
    return stoll(s);
}

int main() {
    int64_t ans = 0;
    vector<int> v {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    do {
        int64_t tmp = magic_sum(v);
        if (tmp > ans)
            ans = tmp;
    } while (next_permutation(v.begin(), v.end()));
    cout << ans << endl;
}   // 6531031914842725